﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TeamPrasioliteWarGame.Models;

namespace TeamPrasioliteWarGame.Admin
{
    public partial class ManageUsersPossessions : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var userId = Request.Params["userId"];
            Entities db = new Entities();
            using (db)
            {
                var user = db.AspNetUsers.SingleOrDefault(u => u.Id == userId);
                if (user != null)
                {
                    this.PageTitle.Text = "Manage user - " + user.UserName;
                }
            }
        }

        // The return type can be changed to IEnumerable, however to support
        // paging and sorting, the following parameters must be added:
        //     int maximumRows
        //     int startRowIndex
        //     out int totalRowCount
        //     string sortByExpression
        public IQueryable<UserBuilding> GridViewUserBuildings_GetData()
        {
            var userId = Request.Params["userId"];
            Entities db = new Entities();
            return db.UserBuildings.Include("Building").Where(u => u.UserId == userId).OrderBy(u => u.BuildingLevel);
        }

        // The id parameter name should match the DataKeyNames value set on the control
        public void GridViewUserBuildings_UpdateItem(int buildingId)
        {
            string userId = Request.Params["userId"];
            Entities db = new Entities();
            UserBuilding item = db.UserBuildings.
                SingleOrDefault(u => u.UserId == userId && u.BuildingId == buildingId);
            // Load the item here, e.g. item = MyDataLayer.Find(id);
            if (item == null)
            {
                // The item wasn't found
                ModelState.AddModelError("", String.Format("Item with id {0} was not found", userId));
                return;
            }
            TryUpdateModel(item);
            if (ModelState.IsValid)
            {
                // Save changes here, e.g. MyDataLayer.SaveChanges();
                db.SaveChanges();
            }
        }

        // The return type can be changed to IEnumerable, however to support
        // paging and sorting, the following parameters must be added:
        //     int maximumRows
        //     int startRowIndex
        //     out int totalRowCount
        //     string sortByExpression
        public IQueryable<UserUnit> GridViewUserUnits_GetData()
        {
            var userId = Request.Params["userId"];
            Entities db = new Entities();
            return db.UserUnits.Include("GameUnit").Where(u => u.UserId == userId).OrderBy(u => u.Count);
        }

        // The id parameter name should match the DataKeyNames value set on the control
        public void GridViewUserUnits_UpdateItem(int unitId)
        {
            string userId = Request.Params["userId"];
            Entities db = new Entities();
            UserUnit item = db.UserUnits.SingleOrDefault(u => u.AspNetUser.Id == userId && u.GameUnit.Id == unitId);
            // Load the item here, e.g. item = MyDataLayer.Find(id);
            if (item == null)
            {
                // The item wasn't found
                ModelState.AddModelError("", String.Format("Item with id {0} was not found", userId));
                return;
            }
            TryUpdateModel(item);
            if (ModelState.IsValid)
            {
                // Save changes here, e.g. MyDataLayer.SaveChanges();
                db.SaveChanges();
            }
        }

        // The id parameter name should match the DataKeyNames value set on the control
        public void GridViewUserBuildings_DeleteItem(int buildingId)
        {
            string userId = Request.Params["userId"];
            Entities db = new Entities();
            var userBuilding = db.UserBuildings.SingleOrDefault(b => b.UserId == userId && b.BuildingId == buildingId);
            db.UserBuildings.Remove(userBuilding);
            db.SaveChanges();
        }

        // The id parameter name should match the DataKeyNames value set on the control
        public void GridViewUserUnits_DeleteItem(int unitId)
        {
            string userId = Request.Params["userId"];
            Entities db = new Entities();
            UserUnit item = db.UserUnits.SingleOrDefault(u => u.AspNetUser.Id == userId && u.GameUnit.Id == unitId);
            db.UserUnits.Remove(item);
            db.SaveChanges();
        }

        // The id parameter should match the DataKeyNames value set on the control
        // or be decorated with a value provider attribute, e.g. [QueryString]int id
        public object DetailsViewUserResourses_GetItem()
        {
            var userId = Request.Params["userId"];
            Entities db = new Entities();
            return db.ExtendUsers.SingleOrDefault(u => u.UserId == userId);
        }

        // The id parameter name should match the DataKeyNames value set on the control
        public void DetailsViewUserResourses_UpdateItem(int? extenderUserId)
        {
            string userId = Request.Params["userId"];
            Entities db = new Entities();
            var user = db.ExtendUsers.SingleOrDefault(u => u.UserId == userId);
            ExtendUser item = user;
            // Load the item here, e.g. item = MyDataLayer.Find(id);
            if (item == null)
            {
                // The item wasn't found
                ModelState.AddModelError("", String.Format("Item with id {0} was not found", extenderUserId));
                return;
            }
            TryUpdateModel(item);
            if (ModelState.IsValid)
            {
                // Save changes here, e.g. MyDataLayer.SaveChanges();
                db.SaveChanges();
            }
        }
    }
}